﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Model;
using Ecreate.Business;

namespace Ecreate.Web.systemanage.customer
{
    public partial class customerlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string addinsert="/systemanage/customer/customercrud.aspx?action=insert&areacode="+this.Request.QueryString["areacode"] ;
                this.hl_addcustomer.NavigateUrl = addinsert;

                utility.SessionObject session = utility.SessionManage.getSession(this);
                if (session.Admin.Group.Grouplevel.Flevel == 0)
                {
                    //this.hl_addcustomer.Attributes["onclick"] = HtmlHelper.GetMessage(this,"管理员不允许添加销售商!");
                }
                if (session.Admin.Group.Grouplevel.Flevel == 3)
                {
                    this.hl_addcustomer.Attributes["onclick"] = HtmlHelper.GetMessage(this,"宣传员不允许添加销售商!");
                }

                CustomerService service = new CustomerService();
                string areacode = Request.QueryString["areacode"];
                this.hid_areacode.Value = areacode;
                if (!String.IsNullOrEmpty(areacode))
                {
                    AreaService areaservice = new AreaService();
                    t_area area = areaservice.getAreaByID(areacode);
                    this.lbl_areaname.Text = area.AreaName + "：";
                    this.hid_areacode.Value = areacode;
                    IList<t_customer> list = service.getCustomersByAreaCode(areacode);
                    this.GridView1.DataSource = list;
                    this.GridView1.DataBind();
                }
                else
                {




                    bindGrid();
                    //IList<t_customer> list = service.getAll();
                    //this.GridView1.DataSource = list;
                    //this.GridView1.DataBind();


                }
            }
        }

        private void bindGrid()
        {
            utility.SessionObject s = utility.SessionManage.getSession(this);
            string hql = " select * from t_customer t where 1=1 ";
            string level = s.Admin.FLevel.ToString();
            string levelid = s.Admin.Id;
            //地级主管
            if (level == "1")
            {
                hql += "  and (   t.level_one_userid='" + levelid + "'";
                hql += "   or (   t.level_two_userid in (select userid  from t_sysuser where parentuser='" + levelid + "')   ";
                hql += ") )";
            }
            //
            if (level == "2")
            {
                hql += " and level_two_userid='" + levelid + "'";
            }
            System.Data.DataTable dt = Ecreate.Data.FactoryDBEngine.GetInstance().GetDataTable(hql);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();


        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            //CustomerService service = new CustomerService();
            //string areacode = this.hid_areacode.Value;
            //if (!String.IsNullOrEmpty(areacode))
            //{
            //    IList<t_customer> list = service.getCustomersByAreaCode(areacode);
            //    this.GridView1.DataSource = list;
            //    this.GridView1.DataBind();
            //}
            this.bindGrid();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string url="/systemanage/customer/customer_insert.aspx?areacode={0}";
            url = string.Format(url, this.hid_areacode);
            Response.Redirect(url);
        }
    }
}
